package com.hp.activiti.controller;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hp.activiti.annotation.ApiOpt;
import com.hp.activiti.bean.AuthUser;
import com.hp.activiti.bean.HistoryData;
import com.hp.activiti.bean.OptTypeEnum;
import com.hp.activiti.bean.Process;
import com.hp.activiti.bean.Result;
import com.hp.activiti.model.Approve;
import com.hp.activiti.model.UserInfo;
import com.hp.activiti.service.ApproveService;
import com.hp.activiti.utils.Base64Utils;

@RestController
@RequestMapping(value="/appro")
public class ApproveController {

	@Autowired
	private ApproveService approveService;

	/**
	 * 上传流程文件
	 * @param uploadfile
	 * @return
	 */
	@PostMapping(value="/uploadworkflow")
	public Result uploadworkflow(@RequestParam MultipartFile uploadfile){
		return approveService.uploadworkflow(uploadfile);
	}
	
	
	/**
	 * 获取所有的流程定义文件信息
	 * @return
	 */
	@GetMapping(value="/queryProcessDefinition")
	public List<Process> queryAllLeaveApply(){
		return approveService.findAll();
	}
	
	/**
	 * 启动请假流程
	 * @param appr
	 * @return
	 */
	@PostMapping(value="/startApprove")
	public Result startApprove(Approve appr){
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		UserInfo user = userInfo.getUser();
		return approveService.startApprove(appr,user);
	}
	
	
	/**
	 * 获取待办任务
	 * 根据登录用户获取用户的角色，根据角色获取待审核信息
	 */
	@GetMapping(value="/queryPendingApproval")
	public List<Approve> queryPendingApproval(){
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		UserInfo user = userInfo.getUser();
		return approveService.queryPendingApproval(user);
	}
	
	
	/**
	 * 获取调整申请信息
	 * @Description:
	 * @date 2019年6月21日 下午5:12:29
	 */
	@GetMapping(value="/queryChangeApproval")
	public List<Approve> queryChangeApproval(){
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		UserInfo user = userInfo.getUser();
		return approveService.queryChangeApproval(user);
	}
	
	/**
	 * 完成任务调整
	 * @Description:
	 * @date 2019年6月21日 下午5:35:12
	 */
	@PostMapping(value="/complateChangeApply/{approFlag}")
	public Result complateChangeApply(@PathVariable String approFlag,Approve appr){
		return approveService.complateChangeApply(approFlag,appr);
	}
	
	
	/**
	 * 流程审批
	 */
	@PostMapping(value="/complateApproval")
	public Result complateApproval(@RequestParam String processDefinitionId, @RequestParam String taskDefinitionKey, @RequestParam String taskId,@RequestParam String approveFlag){
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		UserInfo user = userInfo.getUser();
		return approveService.complateApproval(processDefinitionId,taskDefinitionKey,taskId,approveFlag,user);
	}
	
	/**
	 * 
	 * @Description:获取当前用户已申请的所有信息
	 * @date 2019年6月21日 上午9:34:54
	 */
	@GetMapping(value="/queryApplyData")
	public List<Approve> queryApplyData(){
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		UserInfo user = userInfo.getUser();
		return approveService.queryApplyData(user);
	}
	
	/**
	 * 获取流程记录
	 * @Description:
	 * @date 2019年6月21日 上午10:35:01
	 */
	@GetMapping(value="/queryHistoryTask")
	public List<HistoryData> queryHistoryTask(@RequestParam String processInstanceId){
		return approveService.queryHistoryTask(processInstanceId);
	}
	
	/**
	 * 获取流程跟踪图片信息
	 * @throws Exception 
	 * @Description:
	 * @date 2019年6月26日 上午9:36:52
	 */
	@GetMapping(value="/getProcessImage/{processInstanceId}")
	public String getProcessImage(HttpServletRequest request, HttpServletResponse resp,@PathVariable String processInstanceId) throws Exception{
		byte[] processImage = approveService.getFlowImgByProcInstId(processInstanceId);
        InputStream in = new ByteArrayInputStream(processImage);;
        return Base64Utils.ioToBase64(in);
	}
	
}
